<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-header">
        <h1>欢迎登录</h1>
      </div>
      <el-card class="login-card">
        <el-form ref="loginForm" :model="admin" :rules="loginRules" label-width="80px" class="login-form">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="admin.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="admin.password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="password">
            <el-input type="text" v-model="admin.captchaInput" placeholder="请输入验证码" style="width: 160px"></el-input>
            <el-image style="width:120px;height:38px" :src="imageBase64Data" @click="getCaptcha"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="login-button" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import {ref} from 'vue';
import {ElButton, ElCard, ElForm, ElFormItem, ElInput, ElMessage} from 'element-plus';
import adminApi from "@/api/adminApi";
import router from "@/router";
import {useTokenStore} from "@/stores/token.js";

const tokenStore = useTokenStore()

// 登录表单数据
const admin = ref({
  username: '',
  password: '',
  key: '',
  captchaInput: ''
});
const imageBase64Data = ref('');
// 表单验证规则
const loginRules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
  ],
};

// 处理登录逻辑
function login() {
  adminApi.login(admin.value)
      .then(resp => {
        if (resp.code == 10000) {
          //弹出提示信息
          ElMessage.success({
            message: resp.msg,
            type: "success",
            duration: 1200,
            onClose: () => {
                  tokenStore.updateToken(resp.data)
                  router.push('/admin/index');
              //跳转到首页
            }
          });
          //保存jwt到sessionStorage


        } else {
          ElMessage.error(resp.msg);
        }

      })
}

function getCaptcha() {
  adminApi.captcha()
      .then(resp => {
        admin.value.key = resp.data.key;
        imageBase64Data.value = resp.data.imageBase64Data

      })
}

getCaptcha();
</script>

<style scoped>
.login-page {
  background-image: url('../../assets/img.jpg');
  /* 替换为你的背景图片URL */
  background-size: cover;
  background-position: center;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-container {
  text-align: center;
}

.login-header {
  color: #ffffff;
  /* 根据背景图片调整颜色 */
  margin-bottom: 30px;
}

.login-header h1 {
  font-size: 24px;
  font-weight: bold;
}

.login-card {
  width: 400px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.8);
  /* 半透明背景 */
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.login-form {
  margin-top: 0;
}

.login-button {
  width: 100%;
}
</style>